package com.zhiwei.version.git.service;

import com.zhiwei.version.git.entity.GitCommitLog;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;

import java.io.IOException;
import java.util.List;

/**
 * function GIT仓库服务
 * author zhiwei.yang
 * time 2020-3-4-11:06
 */
public interface GitRepositoryService {

    /**
     * 仓库克隆: git clone
     * @param repoUrl 远程仓库地址
     * @param cloneDir 本地克隆路径(目录内容为空)
     * @param provider 用户凭证
     * @return
     * @throws GitAPIException
     */
    Git cloneRepository(String repoUrl, String cloneDir, CredentialsProvider provider) throws GitAPIException;

    /**
     * 提交本地仓库记录: git commit -a -m '{msg}'
     * @param git git客户都按
     * @param message 提交消息
     * @param provider  用户凭证
     * @throws GitAPIException
     */
    void commitRepository(Git git, String message, CredentialsProvider provider) throws GitAPIException;

    /**
     * 推送到远程仓库: git push (默认分支推送)
     * @param git git客户端
     * @param provider 用户凭证
     * @throws GitAPIException
     * @throws IOException
     */
    void pushRepository(Git git, CredentialsProvider provider) throws GitAPIException, IOException;

    /**
     * 推送到远程仓库
     * @param git git客户端
     * @param branch 分知名
     * @param provider 用户凭证
     * @throws GitAPIException
     * @throws IOException
     */
    void pushRepository(Git git, String branch, CredentialsProvider provider) throws GitAPIException, IOException;

    /**
     * 读取本地Git仓库
     * @param localGitDir 本地Git仓库目录
     * @return
     * @throws IOException
     */
    Repository getLocalRepository(String localGitDir) throws IOException;

    /**
     * 读取本地Git客户端
     * @param localGitDir 本地Git仓库目录
     * @return
     * @throws IOException
     */
    Git getLocalGit(String localGitDir) throws IOException;

    /**
     * 读取仓库日志
     * @param git git仓库
     * @return
     * @throws IOException
     */
    List<GitCommitLog> getRepositoryLog(Git git) throws IOException, GitAPIException;

    /**
     * 读取指定提交开始的仓库日志
     * @param git git仓库
     * @param commitId 提交ID
     * @return
     * @throws IOException
     */
    List<GitCommitLog> getSinceCommitRepositoryLog(Git git, String commitId) throws IOException, GitAPIException;

    /**
     *读取指定分支指定提交开始的仓库日志
     * @param git git仓库
     * @param branch 分知名
     * @param commitId 提交ID
     * @return
     * @throws IOException
     */
    List<GitCommitLog> getBranchSinceCommitRepositoryLog(Git git, String branch, String commitId) throws IOException, GitAPIException;
}
